package com.base.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.base.po.voip.UsersConfig;
import com.base.po.voip.UsersConfigNew;
import com.base.po.voip.UsersRebateConfig;
import com.base.service.UsersConfigService;
import com.base.service.UsersRebateConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.base.comm.CommContent;
import com.base.po.voip.Users;
import com.base.service.LogsService;
import com.base.service.UsersService;
import com.base.utils.IP;

import java.net.URLEncoder;

/**
 * 账户配置信息 修改
 * 
 * @author f 优化日期：10-11
 *
 */
@Controller
@RequestMapping("/admin")
public class AllocationsUpdateController {

	@Autowired
	private LogsService logsService; // 日志Service
	@Autowired
	private UsersService usersService; // 账户信息Service
	@Autowired
	private UsersConfigService usersConfigService;
	@Autowired
	private UsersRebateConfigService usersRebateConfigService;

	// 账户信息 修改
	@RequestMapping(value = "/allocations_do_update", method = RequestMethod.POST)
	public @ResponseBody int doUpdate(HttpServletRequest request, Users users) {
		HttpSession session = request.getSession(false); // 获取session
		if (session == null) {
			return -1;
		}
		Object obj_id = session.getAttribute("adminUserId"); // 登录ID
		Object obj_name = session.getAttribute("userNick"); // 登录名称
		if (obj_id == null || obj_name == null) {
			return -1;
		}

		Users nowUser = usersService.selectByPrimaryKey((String) obj_id);

		if (nowUser.getTbname() != null && users.getTbname() != null && nowUser.getTbname().equals(users.getTbname())){
			users.setTbname(null);
		}
		if (users.getTbname() == null || users.getTbname().equals("")){
			users.setTbname(null);
		}

		if (users.getTbname() != null && !users.getTbname().isEmpty()) {
			try {
				users.setTbname(URLEncoder.encode(users.getTbname(), "UTF-8"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		logsService.insert(CommContent.TYPE_UPDATE, CommContent.LOGS_UPDATE + "账户配置信息", (String) obj_name, true,
				IP.getIpAddr(request));
		return usersService.updateByPrimaryKeySelective(users); // 修改
	}

	// 账户信息 修改
	@RequestMapping(value = "/other_allocations_do_update", method = RequestMethod.POST)
	public @ResponseBody int otherDoUpdate(HttpServletRequest request, Integer id, String mgjUid, String mgjAppkey,
										   String mgjAppsecret, String klUnionid, String snAppkey, String snAppsecret) {
		HttpSession session = request.getSession(false); // 获取session
		if (session == null) {
			return -1;
		}
		Object obj_id = session.getAttribute("adminUserId"); // 登录ID
		Object obj_name = session.getAttribute("userNick"); // 登录名称
		if (obj_id == null || obj_name == null) {
			return -1;
		}

		UsersRebateConfig config = usersRebateConfigService.selectByPrimaryKey(id);
		config.setId(id);
		config.setParentid((String)obj_id);
		config.setMgjUid(mgjUid);
		config.setMgjAppkey(mgjAppkey);
		config.setSnAppsecret(mgjAppsecret);
		config.setKlUnionid(klUnionid);
		config.setSnAppkey(snAppkey);
		config.setSnAppsecret(snAppsecret);
		int code = 0;
		if (config == null){
			code = usersRebateConfigService.insert(config);
		}else {
			code = usersRebateConfigService.updateByPrimaryKeySelective(config);
		}

		logsService.insert(CommContent.TYPE_UPDATE, CommContent.LOGS_UPDATE + "账户配置信息", (String) obj_name, true,
				IP.getIpAddr(request));
		return code;
	}

	// 账户信息 修改
	@RequestMapping(value = "/wx_allocations_do_update", method = RequestMethod.POST)
	public @ResponseBody int wxDoUpdate(HttpServletRequest request, Integer id, String wxMpAppid, String wxMpAppsecret,
										   String wxMpQRCode, String wxDevelopersAppid, String wxDevelopersAppsecret) {
		HttpSession session = request.getSession(false); // 获取session
		if (session == null) {
			return -1;
		}
		Object obj_id = session.getAttribute("adminUserId"); // 登录ID
		Object obj_name = session.getAttribute("userNick"); // 登录名称
		if (obj_id == null || obj_name == null) {
			return -1;
		}

        UsersConfig config = usersConfigService.selectByPrimaryKey(id);
        if (id == null || id.equals("")){
            config = new UsersConfig();
        }else {
            config.setId(id);
        }
        config.setParentid((String)obj_id);
        config.setWxMpAppid(wxMpAppid);
        config.setWxMpAppsecret(wxMpAppsecret);
        config.setWxMpQRCode(wxMpQRCode);
        config.setWxDevelopersAppid(wxDevelopersAppid);
        config.setWxDevelopersAppsecret(wxDevelopersAppsecret);
        int code = 0;
        if (id == null || id.equals("")){
            code = usersConfigService.insert(config);
        }else {
            code = usersConfigService.updateByPrimaryKeySelective(config);
        }

		logsService.insert(CommContent.TYPE_UPDATE, CommContent.LOGS_UPDATE + "账户配置信息", (String) obj_name, true,
				IP.getIpAddr(request));
		return code;
	}

	// 账户信息 修改
	@RequestMapping(value = "/member_allocations_do_update", method = RequestMethod.POST)
	public @ResponseBody int memberDoUpdate(HttpServletRequest request, Integer id, Double member365Coupon) {
		HttpSession session = request.getSession(false); // 获取session
		if (session == null) {
			return -1;
		}
		Object obj_id = session.getAttribute("adminUserId"); // 登录ID
		Object obj_name = session.getAttribute("userNick"); // 登录名称
		if (obj_id == null || obj_name == null) {
			return -1;
		}

		UsersConfig config = usersConfigService.selectByPrimaryKey(id);
		if (id == null || id.equals("")){
			config = new UsersConfig();
		}else {
			config.setId(id);
		}

		if (member365Coupon < 5){
			return -2;
		}

		config.setParentid((String)obj_id);
		config.setMember365Coupon(member365Coupon);
		int code = 0;
		if (id == null || id.equals("")){
			code = usersConfigService.insert(config);
		}else {
			code = usersConfigService.updateByPrimaryKeySelective(config);
		}

		logsService.insert(CommContent.TYPE_UPDATE, CommContent.LOGS_UPDATE + "账户配置信息", (String) obj_name, true,
				IP.getIpAddr(request));
		return code;
	}
}
